#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author  : HaiFeng
# @Email   : 24918700@qq.com
# @Time    : 2022/09/11


import os
import sys
import platform
import ctypes
import copy
from .struct import *


class Quote:

    def __init__(self):
        dllpath = os.path.join(os.path.abspath(os.path.dirname(__file__)), "lib64")
        absolute_dllfile_path = os.path.join(dllpath, 'ctp_quote.dll' if 'Windows' in platform.system() else 'libctp_quote.so')
        if not os.path.exists(absolute_dllfile_path):
            print('缺少DLL接口文件')
            return

        # make log dir for api log
        logdir = os.path.join(os.getcwd(), 'log')
        if not os.path.exists(logdir):
            os.mkdir(logdir)
 
        dlldir = os.path.split(absolute_dllfile_path)[0]
        # change work directory
        cur_path = os.getcwd()
        os.chdir(dlldir)

        self.h = CDLL(absolute_dllfile_path)

        self.h.qCreateApi.argtypes = []
        self.h.qCreateApi.restype = c_void_p

        self.h.qCreateSpi.argtypes = []
        self.h.qCreateSpi.restype = c_void_p

        self.api = None
        self.spi = None

        #################### 请求函数 #######################
        [[ range .Fn ]]# [[ .Comment ]]
        self.h.q[[ .FuncName ]].argtypes = [c_void_p[[ range .FuncFields ]], [[ .FieldType|baseType ]][[ end ]]]
        self.h.q[[ .FuncName ]].restype = c_void_p
        [[ end ]]
        os.chdir(cur_path)

    def CreateApi(self):
        self.api = self.h.qCreateApi()
        return self.api

    def CreateSpi(self):
        self.spi = self.h.qCreateSpi()
        #################### 响应函数 #########################
        [[ range .On ]]# [[ .Comment ]]
        self.h.qSetOn[[ .FuncName ]].argtypes = [c_void_p, c_void_p]
        self.h.qSetOn[[ .FuncName ]].restype = None
        self.evOn[[ .FuncName ]] = CFUNCTYPE(None[[ range $i,$v := .FuncFields ]], [[ .FieldType|evBaseType ]][[ end ]])(self.__On[[ .FuncName ]])
        self.h.qSetOn[[ .FuncName ]](self.spi, self.evOn[[ .FuncName ]])
        [[ end ]]
        return self.spi

    #################### 请求函数 #######################
    [[ range .Fn ]]
    def [[ .FuncName ]](self[[ range .FuncFields ]], [[ .FieldName|trimStar ]]:[[.FieldType|fnBaseType]][[ end ]]):
        """ [[ .Comment ]] """
        self.h.q[[ .FuncName ]](self.api[[ range .FuncFields ]], [[ param .FieldType (.FieldName|trimStar) ]][[ end ]])
    [[ end ]]
    #################### 响应函数 #########################
    [[ range .On ]]
    def __On[[ .FuncName ]](self[[ range .FuncFields ]], [[ .FieldName|trimStar ]]:[[.FieldType|fnBaseType]][[ end ]]):
        self.On[[ .FuncName ]]([[ range $i,$v := .FuncFields ]][[if gt $i 0]], [[ end ]][[ onParam .FieldType (.FieldName|trimStar) ]][[ end ]])
    def On[[ .FuncName ]](self[[ range .FuncFields ]], [[ .FieldName|trimStar ]]:[[.FieldType|fnBaseType]][[ end ]]):
        """ [[ .Comment ]] """
        print('===On[[ .FuncName ]]===:[[ range $i,$v := .FuncFields ]][[if gt $i 0]], [[ end ]][[ .FieldName|trimStar ]]:[[.FieldType|fnBaseType]][[ end ]]')
    [[ end ]]